﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace Homework2
{
    public class BadCashException : Exception
    {
        public BadCashException(string message) : base(message) { }
    }
    public class ATM
    {
        private string cardNumber;
        private string pin;
        //private decimal balance;
        private Bank bank;
        public event Action throwBigMoneyWarning;

        public ATM(){
            bank = new Bank();
        }
        public bool Connect(string cardNumber, string enteredPin)
        {
            this.cardNumber = cardNumber;
            this.pin = enteredPin;
            return bank.CheckAccount(cardNumber, enteredPin);
        }

        public decimal CheckBalance()
        {
            return bank.CheckAccountBalance(cardNumber);
        }

        public bool Withdraw(decimal amount)
        {
            if(bank.WithdrawFromAccount(cardNumber, amount))
            {
                if (amount > 10000)
                {
                    throwBigMoneyWarning();
                }
                return true;
            }
            return false;
        }
        private int checkMoney(int amount,int n)
        {
            int fakeMoneyNum = 0;
            int resultMoney = amount / 100;
            Random random1 = new Random();
            while (resultMoney-- > 0)
            {            
                if (random1.Next(100) + 1 < n)
                {
                    fakeMoneyNum++;
                }
            }
            return fakeMoneyNum;
        }
        public void Deposit(decimal amount)
        {
            int fakeMoneyNum = checkMoney((int)amount, 30);
            bank.DepositToAccount(cardNumber, amount-fakeMoneyNum*100);
            if (fakeMoneyNum > 0)
            {
                throw new BadCashException("fakeMoneyNum:" + (fakeMoneyNum * 100).ToString());
            }
        }

        public bool changePin(string newPin)
        {
            return bank.ChangePin(cardNumber, newPin);
        }
        public void Exit()
        {
            cardNumber = null;
            pin = null;
        }
        public decimal GetCreditNum()
        {
            return bank.GetCreditNum(cardNumber);
        }
    }
}
